package com.sk.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.constant.Constants;
import com.sk.mapper.SClassMapper;
import com.sk.pojo.RespBean;
import com.sk.pojo.SClass;
import com.sk.utils.FdfsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service

public class ClassServer {
    String ip = Constants.IP;
    @Autowired
    FdfsUtils fastDFSUtil;
    @Autowired
    SClassMapper sClassMapper;

    public RespBean add(String name, MultipartFile file){
        SClass sClass = new SClass();
        if (file != null) {
            String path = fastDFSUtil.upload(file);
            sClass.setUrl(ip + path);
        }
        sClass.setName(name);
        int i = sClassMapper.insert(sClass);
        if (i > 0) {
            return RespBean.success("添加成功");
        } else {
            return RespBean.error("添加失败");
        }
    }

    public List<SClass> like(String name){
        QueryWrapper<SClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return sClassMapper.selectList(queryWrapper);
    }

    public RespBean delete(Integer id) {
        int i = sClassMapper.deleteById(id);
        if (i > 0) {
            return RespBean.success("删除成功");
        } else {
            return RespBean.error("删除失败");
        }
    }

    public RespBean update(Integer id,String name, MultipartFile file) {
        SClass sClass = new SClass();
        if (file != null) {
            String path = fastDFSUtil.upload(file);
            sClass.setUrl(ip + path);
        }
        sClass.setName(name);
        sClass.setId(id);
        int i = sClassMapper.updateById(sClass);
        if (i > 0) {
            return RespBean.success("修改成功");
        } else {
            return RespBean.error("修改失败");
        }
    }


}
